#!/bin/bash

usage () {
	echo "usage: $0 <file to convert to .3gp>"
	exit
}

if [ $# -ne 1 ]; then usage; fi
if [ ! -r "$1" ]; then usage; fi

if [ -z "$TEMP" ]; then
	TEMP=/tmp
fi

mencoder "$1" -nosound -ovc lavc -lavcopts vcodec=mpeg4 -vf scale=176:-2,expand=176:144 -o $TEMP/tmp.avi -ofps 12
if [ $? -ne 0 ]; then exit; fi

mplayer -vo null -ao pcm:file=$TEMP/tmp.wav -af resample=8000,volume=+4db:sc "$1"
if [ $? -ne 0 ]; then exit; fi

dest=${1/.???/.3gp}
if [ "$dest" = "$1" ]; then dest=$1.3gp; fi
ffmpeg -i $TEMP/tmp.avi -i $TEMP/tmp.wav -b 48k -ac 1 -ab 12.2k -map 0.0 -map 1.0 "$dest"
if [ $? -ne 0 ]; then exit; fi

rm -i $TEMP/tmp.wav $TEMP/tmp.avi
ls -l "$dest"
